package com.littlec.sdk.entity;

import java.util.Hashtable;
import java.util.List;

import com.littlec.sdk.entity.groupinfo.GroupInfo;
import com.littlec.sdk.entity.messagebody.AtTextMessageBody;
import com.littlec.sdk.entity.messagebody.AudioMessageBody;
import com.littlec.sdk.entity.messagebody.FileMessageBody;
import com.littlec.sdk.entity.messagebody.ImageMessageBody;
import com.littlec.sdk.entity.messagebody.LocationMessageBody;
import com.littlec.sdk.entity.messagebody.MessageBody;
import com.littlec.sdk.entity.messagebody.RichTextMessageBody;
import com.littlec.sdk.entity.messagebody.SMSMessageBody;
import com.littlec.sdk.entity.messagebody.TextMessageBody;
import com.littlec.sdk.entity.messagebody.VideoMessageBody;
import com.littlec.sdk.entity.messagebody.VoiceMailMessageBody;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

public class CMMessage implements Parcelable {
	public static final int PREPARED = -1;
	private int id = -1;
	private String packetId; // ��Ϣ�ڴ����е�id

	private String from;// ��Ϣ�ķ�����id

	private String fromNick;
	private String sendName;
	private String uuid;//消息唯一标识
	
	public String getSendName() {
		return sendName;
	}

	public void setSendName(String sendName) {
		this.sendName = sendName;
	}

	private String to;// ��Ϣ�Ľ�����id

	private int sendOrRecv;// ���ͻ��ǽ���

	private int contentType;// ��Ϣ���ݵ�����

	private int chatType = -1;// ���Ļ���Ⱥ��

	private int status;// ��Ϣ״̬���ѷ��͡����ڷ��͵ȣ�

	private long time;// ������Ϣʱ����Ϣʱ��

	private long guid;
	private transient int progress = 0;
	private MessageBody messageBody;// �ı���ͼƬ����������Ƶ��λ�õ���

	private GroupInfo groupInfo;
	private String messageId;// 发送的消息的唯一标识别

	private Hashtable<String, Object> attributes = null;
	public CMMessage(MessageBody messageBody) {
		this.messageBody = messageBody;
	}

	public CMMessage() {

	}

	public static final Parcelable.Creator<CMMessage> CREATOR = new Parcelable.Creator() {
		public CMMessage createFromParcel(Parcel source) {
			return new CMMessage(source);
		}

		public CMMessage[] newArray(int size) {
			return new CMMessage[size];
		}

	};

	public CMMessage(int chatType, String to, MessageBody messageBody, int id,
			int sendOrRecv, int contentType, int status, long time) {
		this.chatType = chatType;
		this.to = to;
		this.messageBody = messageBody;
		this.id = id;
		this.sendOrRecv = sendOrRecv;
		this.contentType = contentType;
		this.status = status;
		this.time = time;
	}

	public CMMessage(int chatType, String to, MessageBody messageBody) {
		this.chatType = chatType;
		this.to = to;
		initContentType(messageBody);
	}

	public CMMessage(List<String> mList, MessageBody messageBody) {
		if ((mList == null) || (mList.isEmpty())) {
			throw new IllegalArgumentException("�����߲���Ϊ��");
		}
		StringBuilder sb = new StringBuilder();
		for (String username : mList) {
			if (TextUtils.isEmpty(username)) {
				throw new NullPointerException("�������д���Ϊ�յĶ���");
			}
			sb.append(username + " ");
		}
		sb.deleteCharAt(sb.length() - 1);
		this.chatType = 2;
		this.to = sb.toString();
		initContentType(messageBody);
	}

	public long getGuid() {
		return this.guid;
	}

	public void setGuid(long guid) {
		this.guid = guid;
	}

	public GroupInfo getGroupInfo() {
		return this.groupInfo;
	}

	public void setGroupInfo(GroupInfo groupInfo) {
		this.groupInfo = groupInfo;
	}

	public void setAttribute(String string, Object obj) {
		if (this.attributes == null) {
			this.attributes = new Hashtable();
		}
		this.attributes.put(string, obj);
	}

	public Object getAttribute(String string) {
		if (this.attributes == null) {
			return null;
		}
		return this.attributes.get(string);
	}

	public int getProgress() {
		return this.progress;
	}

	public void setProgress(int progress) {
		this.progress = (progress >= 100 ? 99 : progress);
	}

	public String getFromNick() {
		return this.fromNick;
	}

	public void setFromNick(String fromNick) {
		this.fromNick = fromNick;
	}

	public CMMessage(String packetId) {
		this.packetId = packetId;
	}

	private void initContentType(MessageBody messageBody) {
		this.messageBody = messageBody;
		if ((messageBody instanceof TextMessageBody)) {
			this.contentType = 0;
		} else if ((messageBody instanceof RichTextMessageBody)) {
			this.contentType = 13;
		} else if ((messageBody instanceof AtTextMessageBody)) {
			this.contentType = 11;
		} else if ((messageBody instanceof SMSMessageBody)) {
			this.contentType = 8;
		} else if ((messageBody instanceof AudioMessageBody)) {
			this.contentType = 2;
		} else if ((messageBody instanceof ImageMessageBody)) {
			this.contentType = 1;
		} else if ((messageBody instanceof VideoMessageBody)) {
			this.contentType = 4;
		} else if ((messageBody instanceof VoiceMailMessageBody)) {
			this.contentType = 9;
		} else if ((messageBody instanceof LocationMessageBody)) {
			this.contentType = 10;
		} else if ((messageBody instanceof FileMessageBody))
			this.contentType = 6;
	}

	public int getContentType() {
		return this.contentType;
	}

	public void setContentType(int contentType) {
		this.contentType = contentType;
	}

	public int getChatType() {
		return this.chatType;
	}

	public void setChatType(int chatType) {
		this.chatType = chatType;
	}

	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getPacketId() {
		return this.packetId;
	}

	public void setPacketId(String packetId) {
		this.packetId = packetId;
	}

	public int getSendOrRecv() {
		return this.sendOrRecv;
	}

	public void setSendOrRecv(int sendOrRecv) {
		this.sendOrRecv = sendOrRecv;
	}

	public int getStatus() {
		return this.status;
	}

	public long getTime() {
		return this.time;
	}

	public void setTime(long time) {
		this.time = time;
	}

	public MessageBody getMessageBody() {
		if (this.messageBody == null) {
			return new MessageBody() {
				public void writeToParcel(Parcel dest, int flags) {
				}

				public int describeContents() {
					return 0;
				}

				public String getContent() {
					return null;
				}

			};
		}
		return this.messageBody;
	}

	public void setMessageBody(MessageBody messageBody) {
		this.messageBody = messageBody;
	}

	public String getFrom() {
		return this.from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getTo() {
		return this.to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	private CMMessage(Parcel paramParcel) {
		this.id = paramParcel.readInt();
		this.packetId = paramParcel.readString();
		this.from = paramParcel.readString();
		this.fromNick = paramParcel.readString();
		this.to = paramParcel.readString();
		this.sendOrRecv = paramParcel.readInt();
		this.contentType = paramParcel.readInt();
		this.chatType = paramParcel.readInt();
		this.status = paramParcel.readInt();

		this.guid = paramParcel.readLong();
		this.time = paramParcel.readLong();
		this.progress = paramParcel.readInt();
		this.messageBody = ((MessageBody) paramParcel
				.readParcelable(CMMessage.class.getClassLoader()));
		this.groupInfo = ((GroupInfo) paramParcel
				.readParcelable(GroupInfo.class.getClassLoader()));

		this.attributes = new Hashtable();
		paramParcel.readMap(this.attributes, null);
	}

	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(this.id);
		dest.writeString(this.packetId);
		dest.writeString(this.from);
		dest.writeString(this.fromNick);
		dest.writeString(this.to);
		dest.writeInt(this.sendOrRecv);
		dest.writeInt(this.contentType);
		dest.writeInt(this.chatType);
		dest.writeInt(this.status);

		dest.writeLong(this.time);
		dest.writeLong(this.guid);
		dest.writeInt(this.progress);
		dest.writeParcelable(this.messageBody, flags);
		dest.writeParcelable(this.groupInfo, flags);

		dest.writeMap(this.attributes);
	}

	public int describeContents() {
		return 0;
	}

	public String getMessageId() {
		return messageId;
	}

	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

}
